package com.yanyilu.basing.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyilu.basing.common.R;
import com.yanyilu.basing.common.ResultCode;
import com.yanyilu.basing.entity.AdminUser;
import com.yanyilu.basing.entity.CheckInRecord;
import com.yanyilu.basing.jwt.Audience;
import com.yanyilu.basing.mapper.AdminUserMapper;
import com.yanyilu.basing.service.IAdminUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yanyilu.basing.service.ILeaderboardService;
import com.yanyilu.basing.util.JwtTokenUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sxx
 * @since 2020-10-27
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {
    @Autowired
    AdminUserMapper adminUserMapper;

    @Autowired
    Audience audience;

    @Override
    public R adminAdd(AdminUser adminUser){
        Integer role = adminUser.getRole();
        if(0 == role){
            return R.error("不可添加超级管理员");
        }
        Integer sf = adminUserMapper.insert(adminUser);
        if(sf == 1){
            return R.ok();
        }else {
            return R.ok(ResultCode.SYSTEM_INNER_ERROR);
        }

    }

    @Override
    public R adminDelete(Integer adminUserId){
        Integer sf = adminUserMapper.deleteById(adminUserId);
        System.out.println(sf);
        if(sf == 1){
            return R.ok();
        }else {
            return R.ok(ResultCode.SYSTEM_INNER_ERROR);
        }
    }

    @Override
    public R adminUpdate(AdminUser adminUser){
        Integer sf = adminUserMapper.updateById(adminUser);
        if(sf == 1){
            return R.ok();
        }else {
            return R.ok(ResultCode.SYSTEM_INNER_ERROR);
        }
    }

    @Override
    public R adminSelectList(Integer page,Integer limit,String adminUserName){
        PageHelper.startPage(page,limit);
        List<AdminUser> list = adminUserMapper.selectByAdminUserName(adminUserName);
        PageInfo<AdminUser> pageInfo = new PageInfo<>(list);
        return R.ok().put("adminUserList",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    @Override
    public R adminSelectList(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<AdminUser> list = adminUserMapper.selectByAllAdminUser();
        PageInfo<AdminUser> pageInfo = new PageInfo<>(list);
        return R.ok().put("adminUserList",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    /**
     * 登录
     * @param adminUser
     * @return
     */
    @Override
    public R login(AdminUser adminUser) {
        AdminUser adminUser1 = adminUserMapper.selectByUserName(adminUser.getAdminUserName());
        String oPassword = adminUser.getAdminUserPassword();
        if(null != oPassword && oPassword.trim().length() != 0){
            if(oPassword.equals(adminUser1.getAdminUserPassword())){
                return R.ok().put("adminUser",adminUser1).put("token", JwtTokenUtil.createJWT(adminUser1.getAdminUserId().toString(),adminUser1.getAdminUserName(),adminUser1.getRole().toString(),audience));
            }else {
                return R.error("用户名或密码错误！");
            }
        }else{
            return R.error("请输入密码！");
        }
    }

    /**
     * 查询全表接口
     * @param page
     * @param tableName
     * @return
     */
    @Override
    public R selectByAllTableContent(Integer page,String tableName) {
        PageHelper.startPage(page,10);
        List<Map<String,Object>> list = adminUserMapper.selectByAllTableContent(tableName);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    @Override
    public R deleteByAllId(String tableName, String tableNameIdWord, Integer tableNameId) {
        adminUserMapper.deleteByAllId(tableName,tableNameIdWord,tableNameId);
        return R.ok();
    }


}
